<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <template>
            <a-col :lg="14" :md="14" :sm="24" :xl="14">
              <a-form-item label="时间段">
                <j-date v-model="queryParam.createTime_begin" :show-time="true" class="query-group-cust" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date v-model="queryParam.createTime_end" :show-time="true" class="query-group-cust" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <span class="table-page-search-submitButtons" style="float: left;overflow: hidden;">
              <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
              <a-button icon="reload" style="margin-left: 8px" type="primary" @click="searchReset">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="download" type="primary" @click="handleExportXls('设备上传数据')">导出</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange">

        <template slot="dataSlot" slot-scope="dataMaps">
          <span v-for="data in dataMaps">
            <span style="font-weight: bold">{{data.name}}</span>：{{data.value}}&nbsp;
            <a-tag :color="alarmLevelOptions[data.alarmLevel].color">
              <span v-if="alarmLevelOptions[data.alarmLevel].value === 0">{{alarmLevelOptions[data.alarmLevel].text}}</span>
              <span v-else>{{alarmLevelOptions[data.alarmLevel].text}}告警</span>
            </a-tag>
          </span>
        </template>

        <template slot="reportModeSlot" slot-scope="type">
          <a-tag v-if="type===1" color="green">上报</a-tag>
          <a-tag v-else color="gold">手动</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">查看</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
        <data-report-modal ref="modalForm" @ok="modalFormOk"></data-report-modal>
      </a-table>
    </div>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JDate from '@/components/jeecg/JDate.vue'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import DataReportModal from '@views/iotcp/message/dataReport/modules/DataReportModal'
import {getAction} from '@api/manage'

export default {
  name: 'DataReport',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    DataReportModal,
    JDictSelectTag,
    JDate,
    JSearchSelectTag
  },
  props: {
    instanceId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      description: '设备上传数据管理页面',
      alarmLevelOptions: [],
      queryParam: {instanceId: this.instanceId},
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '实例设备',
          align: 'center',
          dataIndex: 'instanceId_dictText'
        },
        {
          title: '数据',
          align: 'left',
          dataIndex: 'dataMap',
          scopedSlots: {customRender: 'dataSlot'}
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'reportMode',
          scopedSlots: {customRender: 'reportModeSlot'}
        },
        {
          title: '日期',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: '/message/data/list',
        delete: '/message/data/delete',
        deleteBatch: '/message/data/deleteBatch',
        exportXlsUrl: '/message/data/exportXls',
        importExcelUrl: 'message/data/importExcel'
      },
      dictOptions: {}
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  mounted() {
    this.getAlarmLevel()
  },
  methods: {
    getAlarmLevel() {
      let scope = this
      getAction('alarm/config/getAlarmLevel').then(data => {
        if (data.success) {
          scope.alarmLevelOptions = data.result
        }
      })
    },
    handleDetail: function (record) {
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title = "详情";
      this.$refs.modalForm.disableSubmit = true;
    },
    searchReset() {
      this.queryParam = {instanceId: this.instanceId}
      this.loadData(1);
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>